package com.example.musicplayer.viewTools;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

public class ViewRotateAnimation {
    public static final String ROTATION_Z = "rotation";
    public static final String ROTATION_X = "rotationX";
    public static final String ROTATION_Y = "rotationY";
    private Animator mAnimator;
    private View mView;
    private String mRotation;
    private int mDuration;
    private int mPivotX;
    private int mPivotY;
    private float mFromDegree;
    private float mToDegree;
    private int mRepeatCount;
    private UpdateListener mUpdateListener;
    private AnimationEndListener mAnimationEndListener;

    public ViewRotateAnimation(View view, String rotation, float fromDegree, float toDegree) {
        if (view != null) {
            switch (rotation) {
                case ROTATION_X:
                case ROTATION_Y:
                case ROTATION_Z:
                    mRotation = rotation;
                    break;
                default:
                    mRotation = ROTATION_Z;
                    break;
            }
            mView = view;
            mDuration = 1000;
            mPivotX = 0;
            mPivotY = 0;
            mFromDegree = fromDegree;
            mToDegree = toDegree;
            mRepeatCount = 0;
            mView.setPivotX(mPivotX);
            mView.setPivotY(mPivotY);
            mView.setCameraDistance(10000f);
            mAnimator = ObjectAnimator.ofFloat(view, mRotation, mFromDegree, mToDegree);
            mAnimator.setDuration(mDuration);
            mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
            ((ValueAnimator) mAnimator).setRepeatCount(mRepeatCount);
            ((ValueAnimator) mAnimator).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    if (mUpdateListener != null) {
                        mUpdateListener.onUpdate(animation);
                    }
                }
            });
            mAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    if (mAnimationEndListener != null) {
                        mAnimationEndListener.onAnimationEnd(animation);
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        }
    }

    public void setDuration(int duration) {
        if (mAnimator != null && duration > 0) {
            mDuration = duration;
            mAnimator.setDuration(mDuration);
        }
    }

    public void setPivotX(int pivotX) {
        if (mView != null) {
            mPivotX = pivotX;
            mView.setPivotX(mPivotX);
        }
    }

    public void setPivotY(int pivotY) {
        if (mView != null) {
            mPivotY = pivotY;
            mView.setPivotY(mPivotY);
        }
    }

    public void setRepeatCount(int repeatCount) {
        if (mAnimator != null) {
            mRepeatCount = repeatCount;
            ((ValueAnimator) mAnimator).setRepeatCount(mRepeatCount);
        }
    }

    public void setInterpolator(TimeInterpolator interpolator) {
        if (mAnimator != null) {
            mAnimator.setInterpolator(interpolator);
        }
    }

    public void setUpdateListener(UpdateListener listener) {
        mUpdateListener = listener;
    }

    public void setAnimationEndListener(AnimationEndListener listener) {
        mAnimationEndListener = listener;
    }

    public void start() {
        if (mAnimator != null) {
            mAnimator.start();
        }
    }

    public void pause() {
        if (mAnimator != null) {
            mAnimator.pause();
        }
    }

    public void resume() {
        if (mAnimator != null) {
            mAnimator.resume();
        }
    }

    public void cancel() {
        if (mAnimator != null) {
            mAnimator.cancel();
        }
    }

    public boolean isStarted() {
        return mAnimator != null && mAnimator.isStarted();
    }

    public interface UpdateListener {
        void onUpdate(ValueAnimator animation);
    }

    public interface AnimationEndListener {
        void onAnimationEnd(Animator animation);
    }
}